package ace.j2se.l1;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Properties;

public class JdbcTest {
	Connection conn;

	protected void openConnection() throws InstantiationException,
			IllegalAccessException, ClassNotFoundException, SQLException {
		Properties props = new Properties();
		props.put("user", "dbhelper");
		props.put("password", "dbhelper");
		props.put("remarksReporting", "true");
		Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
		conn = DriverManager.getConnection(
				"jdbc:oracle:thin:@10.211.55.4:1521:orcl", props);
	}

	public void executeSql(String sql) throws InstantiationException,
			IllegalAccessException, ClassNotFoundException, SQLException {
		openConnection();
		ResultSet rs = null;
		try {
			rs = conn.createStatement().executeQuery(sql);
			ResultSetMetaData rsmd = rs.getMetaData();
			for (int i = 1; i <= rsmd.getColumnCount(); i++) {
				System.out.println(rsmd.getColumnName(i));
				System.out.println(rsmd.getColumnClassName(i));
				System.out.println(rsmd.getColumnTypeName(i));
			}
		} finally {
			if (conn != null) {
				conn.close();
			}
			if (rs != null) {
				rs.close();
			}
		}

	}

	public static void main(String[] args) throws InstantiationException,
			IllegalAccessException, ClassNotFoundException, SQLException {
		JdbcTest jt = new JdbcTest();
		String sql = "select * from T_DATABASE";
		jt.executeSql(sql);
	}
}
